package weaver.interfaces.gyxy.rabbitmq.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.ReturnListener;
import weaver.interfaces.gyxy.rabbitmq.model.MqLogMessage;

import java.io.IOException;
import java.text.SimpleDateFormat;

public class RabbitmqRetrurnListener implements ReturnListener{

    SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public void handleReturn(int replyCode, String replyText,
                             String exchange, String routingKey,
                             AMQP.BasicProperties properties, byte[] body) throws IOException {

        System.out.println("======= handle ======= return ========");
        MqLogMessage mqLogMessage = new MqLogMessage();
        String msg = new String(body,"utf-8");
        JSONObject json  = JSON.parseObject(msg);
        mqLogMessage.setId(json.getString("uid"));
        mqLogMessage.setMessages(msg);
        mqLogMessage.setRoutekey(routingKey);
        mqLogMessage.setResult("1");
        mqLogMessage.setReason(replyText);
        System.out.println("消息发送失败更新："+mqLogMessage.toString());

    }
}
